{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "64d78093",
   "metadata": {},
   "source": [
    "# Pandas的数据转换函数map、apply、applymap"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1c46c163",
   "metadata": {},
   "source": [
    "数据转换函数对比: map、apply、applymap;\n",
    "\n",
    "1. map: 只用于Series，实现每个值->值的映射;\n",
    "2. apply: 用于Series实现每个值的处理，用于DataFrame实现某个轴的Series的处理;\n",
    "3. applymap: 只能用于DataFrame，用于处理该DataFrame的每个元素;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1839902e",
   "metadata": {},
   "source": [
    "## 1. map用于Series值的转换\n",
    "\n",
    "实例：将股票代码英文转换为中文名字\n",
    "\n",
    "Series.map(dict) or Series.map(function)均可"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "4a5d9497",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>日期</th>\n",
       "      <th>公司</th>\n",
       "      <th>收盘</th>\n",
       "      <th>开盘</th>\n",
       "      <th>高</th>\n",
       "      <th>低</th>\n",
       "      <th>交易量</th>\n",
       "      <th>涨跌幅</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2023-07-12</td>\n",
       "      <td>JD</td>\n",
       "      <td>37.41</td>\n",
       "      <td>37.34</td>\n",
       "      <td>37.83</td>\n",
       "      <td>36.91</td>\n",
       "      <td>11.42M</td>\n",
       "      <td>0.0386</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2023-07-11</td>\n",
       "      <td>JD</td>\n",
       "      <td>36.02</td>\n",
       "      <td>35.90</td>\n",
       "      <td>36.39</td>\n",
       "      <td>35.31</td>\n",
       "      <td>6.95M</td>\n",
       "      <td>0.0019</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2023-07-10</td>\n",
       "      <td>JD</td>\n",
       "      <td>35.95</td>\n",
       "      <td>35.30</td>\n",
       "      <td>36.15</td>\n",
       "      <td>35.06</td>\n",
       "      <td>8.33M</td>\n",
       "      <td>0.0053</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2023-07-12</td>\n",
       "      <td>BABA</td>\n",
       "      <td>94.00</td>\n",
       "      <td>94.11</td>\n",
       "      <td>95.03</td>\n",
       "      <td>92.55</td>\n",
       "      <td>23.78M</td>\n",
       "      <td>0.0241</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2023-07-11</td>\n",
       "      <td>BABA</td>\n",
       "      <td>91.79</td>\n",
       "      <td>91.02</td>\n",
       "      <td>92.32</td>\n",
       "      <td>89.01</td>\n",
       "      <td>19.74M</td>\n",
       "      <td>0.0136</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          日期    公司     收盘     开盘      高      低     交易量     涨跌幅\n",
       "0 2023-07-12    JD  37.41  37.34  37.83  36.91  11.42M  0.0386\n",
       "1 2023-07-11    JD  36.02  35.90  36.39  35.31   6.95M  0.0019\n",
       "2 2023-07-10    JD  35.95  35.30  36.15  35.06   8.33M  0.0053\n",
       "3 2023-07-12  BABA  94.00  94.11  95.03  92.55  23.78M  0.0241\n",
       "4 2023-07-11  BABA  91.79  91.02  92.32  89.01  19.74M  0.0136"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "stocks = pd.read_excel('../datas/股票.xlsx')\n",
    "stocks.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "85d2159e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['JD', 'BABA', 'BIDU', 'IQ'], dtype=object)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stocks['公司'].unique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "a7ab4226",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 公司股票代码到中文的映射，注意这里是小写\n",
    "dict_company_names = {\n",
    "    \"bidu\":\"百度\",\n",
    "    \"baba\":\"阿里巴巴\",\n",
    "    \"iq\":\"爱奇艺\",\n",
    "    \"jd\":\"京东\",\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bbd6862e",
   "metadata": {},
   "source": [
    "---\n",
    "**方法1：Series.map(dict)**\n",
    "\n",
    "---"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "09266ee7",
   "metadata": {},
   "outputs": [],
   "source": [
    "stocks[\"公司中文1\"] = stocks[\"公司\"].str.lower().map(dict_company_names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3c6f8d22",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>日期</th>\n",
       "      <th>公司</th>\n",
       "      <th>收盘</th>\n",
       "      <th>开盘</th>\n",
       "      <th>高</th>\n",
       "      <th>低</th>\n",
       "      <th>交易量</th>\n",
       "      <th>涨跌幅</th>\n",
       "      <th>公司中文1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2023-07-12</td>\n",
       "      <td>JD</td>\n",
       "      <td>37.41</td>\n",
       "      <td>37.34</td>\n",
       "      <td>37.83</td>\n",
       "      <td>36.91</td>\n",
       "      <td>11.42M</td>\n",
       "      <td>0.0386</td>\n",
       "      <td>京东</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2023-07-11</td>\n",
       "      <td>JD</td>\n",
       "      <td>36.02</td>\n",
       "      <td>35.90</td>\n",
       "      <td>36.39</td>\n",
       "      <td>35.31</td>\n",
       "      <td>6.95M</td>\n",
       "      <td>0.0019</td>\n",
       "      <td>京东</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2023-07-10</td>\n",
       "      <td>JD</td>\n",
       "      <td>35.95</td>\n",
       "      <td>35.30</td>\n",
       "      <td>36.15</td>\n",
       "      <td>35.06</td>\n",
       "      <td>8.33M</td>\n",
       "      <td>0.0053</td>\n",
       "      <td>京东</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2023-07-12</td>\n",
       "      <td>BABA</td>\n",
       "      <td>94.00</td>\n",
       "      <td>94.11</td>\n",
       "      <td>95.03</td>\n",
       "      <td>92.55</td>\n",
       "      <td>23.78M</td>\n",
       "      <td>0.0241</td>\n",
       "      <td>阿里巴巴</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2023-07-11</td>\n",
       "      <td>BABA</td>\n",
       "      <td>91.79</td>\n",
       "      <td>91.02</td>\n",
       "      <td>92.32</td>\n",
       "      <td>89.01</td>\n",
       "      <td>19.74M</td>\n",
       "      <td>0.0136</td>\n",
       "      <td>阿里巴巴</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          日期    公司     收盘     开盘      高      低     交易量     涨跌幅 公司中文1\n",
       "0 2023-07-12    JD  37.41  37.34  37.83  36.91  11.42M  0.0386    京东\n",
       "1 2023-07-11    JD  36.02  35.90  36.39  35.31   6.95M  0.0019    京东\n",
       "2 2023-07-10    JD  35.95  35.30  36.15  35.06   8.33M  0.0053    京东\n",
       "3 2023-07-12  BABA  94.00  94.11  95.03  92.55  23.78M  0.0241  阿里巴巴\n",
       "4 2023-07-11  BABA  91.79  91.02  92.32  89.01  19.74M  0.0136  阿里巴巴"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stocks.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ad50f593",
   "metadata": {},
   "source": [
    "---\n",
    "**方法二：Series.map(function)**\n",
    "\n",
    "function的参数是Series的每个元素的值\n",
    "\n",
    "---"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "c0dee05a",
   "metadata": {},
   "outputs": [],
   "source": [
    "stocks[\"公司中文2\"] = stocks[\"公司\"].map(lambda x : dict_company_names[x.lower()])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "4c3d2674",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>日期</th>\n",
       "      <th>公司</th>\n",
       "      <th>收盘</th>\n",
       "      <th>开盘</th>\n",
       "      <th>高</th>\n",
       "      <th>低</th>\n",
       "      <th>交易量</th>\n",
       "      <th>涨跌幅</th>\n",
       "      <th>公司中文1</th>\n",
       "      <th>公司中文2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2023-07-12</td>\n",
       "      <td>JD</td>\n",
       "      <td>37.41</td>\n",
       "      <td>37.34</td>\n",
       "      <td>37.83</td>\n",
       "      <td>36.91</td>\n",
       "      <td>11.42M</td>\n",
       "      <td>0.0386</td>\n",
       "      <td>京东</td>\n",
       "      <td>京东</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2023-07-11</td>\n",
       "      <td>JD</td>\n",
       "      <td>36.02</td>\n",
       "      <td>35.90</td>\n",
       "      <td>36.39</td>\n",
       "      <td>35.31</td>\n",
       "      <td>6.95M</td>\n",
       "      <td>0.0019</td>\n",
       "      <td>京东</td>\n",
       "      <td>京东</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2023-07-10</td>\n",
       "      <td>JD</td>\n",
       "      <td>35.95</td>\n",
       "      <td>35.30</td>\n",
       "      <td>36.15</td>\n",
       "      <td>35.06</td>\n",
       "      <td>8.33M</td>\n",
       "      <td>0.0053</td>\n",
       "      <td>京东</td>\n",
       "      <td>京东</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2023-07-12</td>\n",
       "      <td>BABA</td>\n",
       "      <td>94.00</td>\n",
       "      <td>94.11</td>\n",
       "      <td>95.03</td>\n",
       "      <td>92.55</td>\n",
       "      <td>23.78M</td>\n",
       "      <td>0.0241</td>\n",
       "      <td>阿里巴巴</td>\n",
       "      <td>阿里巴巴</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2023-07-11</td>\n",
       "      <td>BABA</td>\n",
       "      <td>91.79</td>\n",
       "      <td>91.02</td>\n",
       "      <td>92.32</td>\n",
       "      <td>89.01</td>\n",
       "      <td>19.74M</td>\n",
       "      <td>0.0136</td>\n",
       "      <td>阿里巴巴</td>\n",
       "      <td>阿里巴巴</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          日期    公司     收盘     开盘      高      低     交易量     涨跌幅 公司中文1 公司中文2\n",
       "0 2023-07-12    JD  37.41  37.34  37.83  36.91  11.42M  0.0386    京东    京东\n",
       "1 2023-07-11    JD  36.02  35.90  36.39  35.31   6.95M  0.0019    京东    京东\n",
       "2 2023-07-10    JD  35.95  35.30  36.15  35.06   8.33M  0.0053    京东    京东\n",
       "3 2023-07-12  BABA  94.00  94.11  95.03  92.55  23.78M  0.0241  阿里巴巴  阿里巴巴\n",
       "4 2023-07-11  BABA  91.79  91.02  92.32  89.01  19.74M  0.0136  阿里巴巴  阿里巴巴"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stocks.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "91113bf2",
   "metadata": {},
   "source": [
    "## 2. apply用于Series和DataFrame的转换\n",
    "* Series.apply(function),函数的参数是每个值\n",
    "* DataFrame.apply(function),函数的参数是Series"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7ca1b7af",
   "metadata": {},
   "source": [
    "**Series.apply(function)**\n",
    "\n",
    "function的参数是Series的每个值\n",
    "\n",
    "---"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "1d1c722b",
   "metadata": {},
   "outputs": [],
   "source": [
    "stocks[\"公司中文3\"] = stocks[\"公司\"].apply(\n",
    "    lambda x : dict_company_names[x.lower()])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "850a00d6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>日期</th>\n",
       "      <th>公司</th>\n",
       "      <th>收盘</th>\n",
       "      <th>开盘</th>\n",
       "      <th>高</th>\n",
       "      <th>低</th>\n",
       "      <th>交易量</th>\n",
       "      <th>涨跌幅</th>\n",
       "      <th>公司中文1</th>\n",
       "      <th>公司中文2</th>\n",
       "      <th>公司中文3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2023-07-12</td>\n",
       "      <td>JD</td>\n",
       "      <td>37.41</td>\n",
       "      <td>37.34</td>\n",
       "      <td>37.83</td>\n",
       "      <td>36.91</td>\n",
       "      <td>11.42M</td>\n",
       "      <td>0.0386</td>\n",
       "      <td>京东</td>\n",
       "      <td>京东</td>\n",
       "      <td>京东</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2023-07-11</td>\n",
       "      <td>JD</td>\n",
       "      <td>36.02</td>\n",
       "      <td>35.90</td>\n",
       "      <td>36.39</td>\n",
       "      <td>35.31</td>\n",
       "      <td>6.95M</td>\n",
       "      <td>0.0019</td>\n",
       "      <td>京东</td>\n",
       "      <td>京东</td>\n",
       "      <td>京东</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2023-07-10</td>\n",
       "      <td>JD</td>\n",
       "      <td>35.95</td>\n",
       "      <td>35.30</td>\n",
       "      <td>36.15</td>\n",
       "      <td>35.06</td>\n",
       "      <td>8.33M</td>\n",
       "      <td>0.0053</td>\n",
       "      <td>京东</td>\n",
       "      <td>京东</td>\n",
       "      <td>京东</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2023-07-12</td>\n",
       "      <td>BABA</td>\n",
       "      <td>94.00</td>\n",
       "      <td>94.11</td>\n",
       "      <td>95.03</td>\n",
       "      <td>92.55</td>\n",
       "      <td>23.78M</td>\n",
       "      <td>0.0241</td>\n",
       "      <td>阿里巴巴</td>\n",
       "      <td>阿里巴巴</td>\n",
       "      <td>阿里巴巴</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2023-07-11</td>\n",
       "      <td>BABA</td>\n",
       "      <td>91.79</td>\n",
       "      <td>91.02</td>\n",
       "      <td>92.32</td>\n",
       "      <td>89.01</td>\n",
       "      <td>19.74M</td>\n",
       "      <td>0.0136</td>\n",
       "      <td>阿里巴巴</td>\n",
       "      <td>阿里巴巴</td>\n",
       "      <td>阿里巴巴</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          日期    公司     收盘     开盘      高      低     交易量     涨跌幅 公司中文1 公司中文2  \\\n",
       "0 2023-07-12    JD  37.41  37.34  37.83  36.91  11.42M  0.0386    京东    京东   \n",
       "1 2023-07-11    JD  36.02  35.90  36.39  35.31   6.95M  0.0019    京东    京东   \n",
       "2 2023-07-10    JD  35.95  35.30  36.15  35.06   8.33M  0.0053    京东    京东   \n",
       "3 2023-07-12  BABA  94.00  94.11  95.03  92.55  23.78M  0.0241  阿里巴巴  阿里巴巴   \n",
       "4 2023-07-11  BABA  91.79  91.02  92.32  89.01  19.74M  0.0136  阿里巴巴  阿里巴巴   \n",
       "\n",
       "  公司中文3  \n",
       "0    京东  \n",
       "1    京东  \n",
       "2    京东  \n",
       "3  阿里巴巴  \n",
       "4  阿里巴巴  "
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stocks.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1edb6c1a",
   "metadata": {},
   "source": [
    "**DataFrame.apply(function)**\n",
    "\n",
    "function的参数是对应轴的Series\n",
    "\n",
    "---"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "ced92efd",
   "metadata": {},
   "outputs": [],
   "source": [
    "stocks[\"公司中文4\"] = stocks.apply(\n",
    "    lambda x : dict_company_names[x[\"公司\"].lower()],\n",
    "    axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "69d5234a",
   "metadata": {},
   "source": [
    "注意这个代码：\n",
    "1. apply实在stocks这个DataFrame上调用;\n",
    "2. lambda x的x是一个Series，因为指定了axis=1所以Series的key是列名，可以用x['公司']获取;\n",
    "---"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "e8b0b3f8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>日期</th>\n",
       "      <th>公司</th>\n",
       "      <th>收盘</th>\n",
       "      <th>开盘</th>\n",
       "      <th>高</th>\n",
       "      <th>低</th>\n",
       "      <th>交易量</th>\n",
       "      <th>涨跌幅</th>\n",
       "      <th>公司中文1</th>\n",
       "      <th>公司中文2</th>\n",
       "      <th>公司中文3</th>\n",
       "      <th>公司中文4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2023-07-12</td>\n",
       "      <td>JD</td>\n",
       "      <td>37.41</td>\n",
       "      <td>37.34</td>\n",
       "      <td>37.83</td>\n",
       "      <td>36.91</td>\n",
       "      <td>11.42M</td>\n",
       "      <td>0.0386</td>\n",
       "      <td>京东</td>\n",
       "      <td>京东</td>\n",
       "      <td>京东</td>\n",
       "      <td>京东</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2023-07-11</td>\n",
       "      <td>JD</td>\n",
       "      <td>36.02</td>\n",
       "      <td>35.90</td>\n",
       "      <td>36.39</td>\n",
       "      <td>35.31</td>\n",
       "      <td>6.95M</td>\n",
       "      <td>0.0019</td>\n",
       "      <td>京东</td>\n",
       "      <td>京东</td>\n",
       "      <td>京东</td>\n",
       "      <td>京东</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2023-07-10</td>\n",
       "      <td>JD</td>\n",
       "      <td>35.95</td>\n",
       "      <td>35.30</td>\n",
       "      <td>36.15</td>\n",
       "      <td>35.06</td>\n",
       "      <td>8.33M</td>\n",
       "      <td>0.0053</td>\n",
       "      <td>京东</td>\n",
       "      <td>京东</td>\n",
       "      <td>京东</td>\n",
       "      <td>京东</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2023-07-12</td>\n",
       "      <td>BABA</td>\n",
       "      <td>94.00</td>\n",
       "      <td>94.11</td>\n",
       "      <td>95.03</td>\n",
       "      <td>92.55</td>\n",
       "      <td>23.78M</td>\n",
       "      <td>0.0241</td>\n",
       "      <td>阿里巴巴</td>\n",
       "      <td>阿里巴巴</td>\n",
       "      <td>阿里巴巴</td>\n",
       "      <td>阿里巴巴</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2023-07-11</td>\n",
       "      <td>BABA</td>\n",
       "      <td>91.79</td>\n",
       "      <td>91.02</td>\n",
       "      <td>92.32</td>\n",
       "      <td>89.01</td>\n",
       "      <td>19.74M</td>\n",
       "      <td>0.0136</td>\n",
       "      <td>阿里巴巴</td>\n",
       "      <td>阿里巴巴</td>\n",
       "      <td>阿里巴巴</td>\n",
       "      <td>阿里巴巴</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          日期    公司     收盘     开盘      高      低     交易量     涨跌幅 公司中文1 公司中文2  \\\n",
       "0 2023-07-12    JD  37.41  37.34  37.83  36.91  11.42M  0.0386    京东    京东   \n",
       "1 2023-07-11    JD  36.02  35.90  36.39  35.31   6.95M  0.0019    京东    京东   \n",
       "2 2023-07-10    JD  35.95  35.30  36.15  35.06   8.33M  0.0053    京东    京东   \n",
       "3 2023-07-12  BABA  94.00  94.11  95.03  92.55  23.78M  0.0241  阿里巴巴  阿里巴巴   \n",
       "4 2023-07-11  BABA  91.79  91.02  92.32  89.01  19.74M  0.0136  阿里巴巴  阿里巴巴   \n",
       "\n",
       "  公司中文3 公司中文4  \n",
       "0    京东    京东  \n",
       "1    京东    京东  \n",
       "2    京东    京东  \n",
       "3  阿里巴巴  阿里巴巴  \n",
       "4  阿里巴巴  阿里巴巴  "
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stocks.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ccd03344",
   "metadata": {},
   "source": [
    "## 3. applymap用于DataFrame所有值的转换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "2c874b32",
   "metadata": {},
   "outputs": [],
   "source": [
    "sub_df = stocks[['收盘','开盘','高','低']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "062d5007",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>收盘</th>\n",
       "      <th>开盘</th>\n",
       "      <th>高</th>\n",
       "      <th>低</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>37.41</td>\n",
       "      <td>37.34</td>\n",
       "      <td>37.83</td>\n",
       "      <td>36.91</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>36.02</td>\n",
       "      <td>35.90</td>\n",
       "      <td>36.39</td>\n",
       "      <td>35.31</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>35.95</td>\n",
       "      <td>35.30</td>\n",
       "      <td>36.15</td>\n",
       "      <td>35.06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>94.00</td>\n",
       "      <td>94.11</td>\n",
       "      <td>95.03</td>\n",
       "      <td>92.55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>91.79</td>\n",
       "      <td>91.02</td>\n",
       "      <td>92.32</td>\n",
       "      <td>89.01</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      收盘     开盘      高      低\n",
       "0  37.41  37.34  37.83  36.91\n",
       "1  36.02  35.90  36.39  35.31\n",
       "2  35.95  35.30  36.15  35.06\n",
       "3  94.00  94.11  95.03  92.55\n",
       "4  91.79  91.02  92.32  89.01"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sub_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "f322b20c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>收盘</th>\n",
       "      <th>开盘</th>\n",
       "      <th>高</th>\n",
       "      <th>低</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>37</td>\n",
       "      <td>37</td>\n",
       "      <td>37</td>\n",
       "      <td>36</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>36</td>\n",
       "      <td>35</td>\n",
       "      <td>36</td>\n",
       "      <td>35</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>35</td>\n",
       "      <td>35</td>\n",
       "      <td>36</td>\n",
       "      <td>35</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>94</td>\n",
       "      <td>94</td>\n",
       "      <td>95</td>\n",
       "      <td>92</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>91</td>\n",
       "      <td>91</td>\n",
       "      <td>92</td>\n",
       "      <td>89</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>90</td>\n",
       "      <td>90</td>\n",
       "      <td>92</td>\n",
       "      <td>89</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>148</td>\n",
       "      <td>147</td>\n",
       "      <td>150</td>\n",
       "      <td>145</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>143</td>\n",
       "      <td>143</td>\n",
       "      <td>144</td>\n",
       "      <td>140</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>142</td>\n",
       "      <td>140</td>\n",
       "      <td>143</td>\n",
       "      <td>140</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     收盘   开盘    高    低\n",
       "0    37   37   37   36\n",
       "1    36   35   36   35\n",
       "2    35   35   36   35\n",
       "3    94   94   95   92\n",
       "4    91   91   92   89\n",
       "5    90   90   92   89\n",
       "6   148  147  150  145\n",
       "7   143  143  144  140\n",
       "8   142  140  143  140\n",
       "9     5    5    5    5\n",
       "10    5    5    5    5\n",
       "11    5    5    5    5"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将这些数字取整数，应用于所有元素\n",
    "sub_df.applymap(lambda x : int(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "5edb0582",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 直接修改原df的这几列\n",
    "stocks.loc[:,['收盘','开盘','高','低']] = sub_df.applymap(lambda x : int(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "45a8d788",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>日期</th>\n",
       "      <th>公司</th>\n",
       "      <th>收盘</th>\n",
       "      <th>开盘</th>\n",
       "      <th>高</th>\n",
       "      <th>低</th>\n",
       "      <th>交易量</th>\n",
       "      <th>涨跌幅</th>\n",
       "      <th>公司中文1</th>\n",
       "      <th>公司中文2</th>\n",
       "      <th>公司中文3</th>\n",
       "      <th>公司中文4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2023-07-12</td>\n",
       "      <td>JD</td>\n",
       "      <td>37</td>\n",
       "      <td>37</td>\n",
       "      <td>37</td>\n",
       "      <td>36</td>\n",
       "      <td>11.42M</td>\n",
       "      <td>0.0386</td>\n",
       "      <td>京东</td>\n",
       "      <td>京东</td>\n",
       "      <td>京东</td>\n",
       "      <td>京东</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2023-07-11</td>\n",
       "      <td>JD</td>\n",
       "      <td>36</td>\n",
       "      <td>35</td>\n",
       "      <td>36</td>\n",
       "      <td>35</td>\n",
       "      <td>6.95M</td>\n",
       "      <td>0.0019</td>\n",
       "      <td>京东</td>\n",
       "      <td>京东</td>\n",
       "      <td>京东</td>\n",
       "      <td>京东</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2023-07-10</td>\n",
       "      <td>JD</td>\n",
       "      <td>35</td>\n",
       "      <td>35</td>\n",
       "      <td>36</td>\n",
       "      <td>35</td>\n",
       "      <td>8.33M</td>\n",
       "      <td>0.0053</td>\n",
       "      <td>京东</td>\n",
       "      <td>京东</td>\n",
       "      <td>京东</td>\n",
       "      <td>京东</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2023-07-12</td>\n",
       "      <td>BABA</td>\n",
       "      <td>94</td>\n",
       "      <td>94</td>\n",
       "      <td>95</td>\n",
       "      <td>92</td>\n",
       "      <td>23.78M</td>\n",
       "      <td>0.0241</td>\n",
       "      <td>阿里巴巴</td>\n",
       "      <td>阿里巴巴</td>\n",
       "      <td>阿里巴巴</td>\n",
       "      <td>阿里巴巴</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2023-07-11</td>\n",
       "      <td>BABA</td>\n",
       "      <td>91</td>\n",
       "      <td>91</td>\n",
       "      <td>92</td>\n",
       "      <td>89</td>\n",
       "      <td>19.74M</td>\n",
       "      <td>0.0136</td>\n",
       "      <td>阿里巴巴</td>\n",
       "      <td>阿里巴巴</td>\n",
       "      <td>阿里巴巴</td>\n",
       "      <td>阿里巴巴</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          日期    公司  收盘  开盘   高   低     交易量     涨跌幅 公司中文1 公司中文2 公司中文3 公司中文4\n",
       "0 2023-07-12    JD  37  37  37  36  11.42M  0.0386    京东    京东    京东    京东\n",
       "1 2023-07-11    JD  36  35  36  35   6.95M  0.0019    京东    京东    京东    京东\n",
       "2 2023-07-10    JD  35  35  36  35   8.33M  0.0053    京东    京东    京东    京东\n",
       "3 2023-07-12  BABA  94  94  95  92  23.78M  0.0241  阿里巴巴  阿里巴巴  阿里巴巴  阿里巴巴\n",
       "4 2023-07-11  BABA  91  91  92  89  19.74M  0.0136  阿里巴巴  阿里巴巴  阿里巴巴  阿里巴巴"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stocks.head()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
